<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：后台首页控制器。
 *
 **/

namespace ZmrxiaoshuoApi\Controller;

use Think\Controller;

class ApiController extends Controller
{
	public function Getid(){
		header('Content-type: text/html;charset=utf-8');
		$data = I('post.');
		$key = $data['key'];
		//判断接口使用权限(10.1号23:59:59过期，密钥559c16d64f983579e8019d92a95d0403)
		if (time()>1506873599) {
			echo '授权密钥已过期!';
		}elseif(empty($key)||$key != md5("shiyu!@#")){
			echo '授权密钥验证失败!';
		}else{
			$novel_name = $data['novel_name'];
			$isset = M("novel")->where("name = '$novel_name'")->find();
			if (!empty($isset)) {
				echo '错误！该小说已存在';
			}else{
				$insData = [
					'name'=>$novel_name,
					'description'=>$data['description'],
					'img'=>$data['img'],
					'mini_img'=>$data['img'],
					'class_id'=>24,
					'class_name'=>'都市小说',
					'other_class_id'=>3,
					'other_class_name'=>'男生',
					'is_shelf'=>0,
					'publish_time'=>date("Y-m-d H:i:s",time())
				];
				$insertId = M("novel")->add($insData);
				if ($insertId) {
					echo '添加小说成功，小说ID为:'.$insertId;
				}else{
					echo '添加失败请重试';
				}
			}
		}
	}

	public function Addchapter(){
		$data = I('post.');
		$key = $data['key'];
		$novel_id = $data['novel_id'];
		$novel_info = M("novel")->where("id = $novel_id")->find();
		$order_set = $data['order_set'];
		$isset = M("novel_chapter_add")->where("novel_id = '$novel_id' and order_set = '$order_set'")->find();
		if (time()>1506873599) {
			echo '授权密钥已过期';
		}elseif(empty($key)||$key != md5("shiyu!@#")){
			echo '授权密钥验证失败';
		}elseif(empty($novel_info)){
			echo '小说不存在，novel_id错误！';
		}elseif(!empty($isset)){
			echo '错误！该小说第'.$order_set.'章已存在！';
		}else{
			$novel_name = $novel_info['name'];
			$insData = [
				'novel_id'=>$novel_id,
				'chapter'=>$data['chapter'],
				'title'=>$data['title'],
				'content'=>$data['content'],
				'novel_name'=>$novel_name,
				'novel_mini_img'=>$novel_info['img'],
				'order_set'=>$order_set,
				'add_time'=>date("Y-m-d H:i:s",time()),
				'word_count'=>$data['word_count']
			];
			if (M("novel_chapter_add")->add($insData)) {
				echo '添加章节成功';
			}else{
				echo '添加章节失败';
			}
		}
	}

	public function test(){
		header('Content-type: text/html;charset=utf-8');
		$url = 'http://test12.double-life.top/ZmrxiaoshuoApi/Api/getid';
		$data = [
			'key'=>'559c16d64f983579e8019d92a95d0403',
			'novel_name'=>'测试',
			'description'=>'描述',
			'img'=>'123'
		];
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt ($ch, CURLOPT_POST, 1 );
		curl_setopt ($ch, CURLOPT_POSTFIELDS, $data );
		$result = curl_exec($ch);
		$info = curl_getinfo ($ch);


		curl_close($ch);
		var_dump($info);
	}

	public function test2(){
		header('Content-type: text/html;charset=utf-8');

		$url = 'http://test12.double-life.top/ZmrxiaoshuoApi/Api/addchapter';
		$data = [
			'key'=>'559c16d64f983579e8019d92a95d0403',
			'novel_id'=>'913',
			'chapter'=>'第1章',
			'title'=>'标题',
			'content'=>'内容',
			'order_set'=>1,
			'word_count'=>100
		];
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt ($ch, CURLOPT_POST, 1 );
		curl_setopt ($ch, CURLOPT_POSTFIELDS, $data );
		$result = curl_exec($ch);
		curl_close($ch);
		var_dump($result);
	}

}